<!--
 * @Description:员工管理
 * @Author: xuxu
 * @Date: 2021-06-24 17:19:57
 * @LastEditors: Morgan
 * @LastEditTime: 2021-07-02 19:59:29
-->

<template>
  <div class="customer-list">
    <div class="search">
      <el-button type="primary" class="btn" @click="add">添加</el-button>

      <el-form :inline="true" :model="searchForm" size="mini" class="demo-form-inline">
        <el-form-item>
          <el-input
            v-model="searchForm.username"
            placeholder="请输入用户名"
          />
        </el-form-item>
        <el-form-item>
          <el-select v-model="searchForm.status" placeholder="请选择状态">
            <el-option label="正常" value="正常" />
            <el-option label="禁用" value="禁用" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="toSearch">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 表格 -->
    <div class="">
      <el-table stripe :data="tableData" size="mini" style="width: 100%">
        <el-table-column prop="id" label="编号" />
        <el-table-column prop="username" label="用户名" />
        <el-table-column prop="realname" label="姓名" />
        <el-table-column prop="telephone" align="center" label="手机号" />
        <!-- <el-table-column prop="gender" label="性别">
          <i v-if="gender==='female'" class="el-icon-female"></i>
          <i v-else class="el-icon-male"></i>
        </el-table-column> -->
        <el-table-column label="性别">
          <template slot-scope="scope">
            <i v-if="scope.row.gender==='female'" class="el-icon-female red_info" />
            <i v-if="scope.row.gender==='male'" class="el-icon-male green_info" />
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.status === '正常' ? 'success' : 'danger'"
            >{{ scope.row.status }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="140">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="mini"
              @click="toEdit(scope.row)"
            >编辑</el-button>
            <el-button
              type="text"
              style="color:#FF0000"
              size="mini"
              @click="toDelete (scope.row.id)"
            >删除</el-button>
            <el-button type="text" size="mini" class="blue_info" @click="toJump(scope.row.id)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <div class="pagination">
      <el-pagination
        :current-page="params.page"
        :page-sizes="[2, 5, 10, 15, 20, 30]"
        :page-size="params.pageSize"
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <div>
      <!-- 模态框 对话框 -->
      <el-dialog title="添加员工信息" :visible.sync="visible" @close="toClose">
        <!-- {{form}} -->
        <el-form ref="ruleForm" :model="form" :rules="rules" size="medium">
          <el-form-item
            prop="username"
            label="用户名"
            :label-width="formLabelWidth"
          >
            <el-input v-model="form.username" autocomplete="off" />
          </el-form-item>
          <el-form-item
            prop="realname"
            label="姓名"
            :label-width="formLabelWidth"
          >
            <el-input v-model="form.realname" autocomplete="off" />
          </el-form-item>
          <el-form-item
            prop="telephone"
            label="手机号"
            :label-width="formLabelWidth"
          >
            <el-input
              v-model.number="form.telephone"
              autocomplete="off"
            />
          </el-form-item>
          <el-form-item
            prop="password"
            label="密码"
            :label-width="formLabelWidth"
          >
            <el-input
              v-model.number="form.password"
              autocomplete="off"
            />
          </el-form-item>
          <el-form-item
            prop="gender"
            label="性别"
            :label-width="formLabelWidth"
          >
            <el-radio-group v-model="form.gender">
              <el-radio label="male">男</el-radio>
              <el-radio label="female">女</el-radio>
            </el-radio-group>
          <!--
            <el-radio v-model="radio.gender" label="1">男</el-radio>
            <el-radio v-model="radio.gender" label="2">女</el-radio>-->
          </el-form-item>
          <el-form-item>
            <el-input v-model="form.roleId" type="hidden" autocomplete="off" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="visible = false">取 消</el-button>
          <el-button type="primary" @click="toSave">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <div>
      <!-- 模态框 对话框 -->
      <el-dialog title="编辑员工信息" :visible.sync="visibletwo" @close="toClose">
        <!-- {{form}} -->
        <el-form ref="ruleForm" :model="form" :rules="rules" size="medium">
          <el-form-item
            prop="username"
            label="用户名"
            :label-width="formLabelWidth"
          >
            <el-input v-model="form.username" autocomplete="off" />
          </el-form-item>
          <el-form-item
            prop="realname"
            label="姓名"
            :label-width="formLabelWidth"
          >
            <el-input v-model="form.realname" autocomplete="off" />
          </el-form-item>
          <el-form-item
            prop="telephone"
            label="手机号"
            :label-width="formLabelWidth"
          >
            <el-input
              v-model.number="form.telephone"
              autocomplete="off"
            />
          </el-form-item>
          <el-form-item
            prop="password"
            label="密码"
            :label-width="formLabelWidth"
          >
            <el-input
              v-model.number="form.password"
              autocomplete="off"
            />
          </el-form-item>
          <el-form-item
            prop="gender"
            label="性别"
            :label-width="formLabelWidth"
          >
            <el-radio-group v-model="form.gender">
              <el-radio label="male" style="font-color:yellow">男</el-radio>
              <el-radio label="female">女</el-radio>
            </el-radio-group>
          <!--
            <el-radio v-model="radio.gender" label="1">男</el-radio>
            <el-radio v-model="radio.gender" label="2">女</el-radio>-->
          </el-form-item>
          <el-form-item
            prop="status"
            label="状态"
            :label-width="formLabelWidth"
          >
            <el-radio-group v-model="form.status">
              <el-radio label="禁用">禁用</el-radio>
              <el-radio label="正常">正常</el-radio>
            </el-radio-group>
            <el-input v-model="form.status" autocomplete="off" />
          </el-form-item>
          <el-form-item>
            <el-input v-model="form.roleId" type="hidden" autocomplete="off" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="visible = false">取 消</el-button>
          <el-button type="primary" @click="toSavetwo">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { pageQuery, addUserWidthRole, saveOrUpdate, deleteById } from '@/api/user'
import { showFileURL, uploadFileURL } from '@/utils/config'
// import { pageQuery as pageQueryPro } from "@/api/user";

export default {
  data() {
    return {
      tableData: [],
      showFileURL,
      uploadFileURL,
      categoryData: [],
      searchForm: {
        username: '',
        status: '',
        roleId: 1
      },
      //  表格展示的数据
      tableData: [],
      // 分页参数
      params: {
        page: 1,
        pageSize: 10
      },
      // 总条数
      total: 0,
      // 加载
      loading: false,
      // 模态框的显示与隐藏
      visible: false,
      visibletwo: false,
      radio: '1',
      // 表单数据
      form: {
        roleId: 1
      },
      // 表单控件标题的宽度
      formLabelWidth: '80px',
      // 模态框的标题
      title: '',
      rules: {
        username: [
          {
            required: true,
            message: '请输入用户名',
            trigger: 'blur'
          }
        ],
        realname: [
          {
            required: true,
            message: '请输入姓名',
            trigger: 'blur'
          }
        ],
        telephone: [
          {
            required: true,
            message: '请输入手机号',
            trigger: 'blur'
          }
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  computed: {},
  watch: {
    // 深度监听分页参数
    params: {
      handler() {
        this.queryData()
      },
      deep: true
    }
  },
  created() {
    this.queryInformationData()
    this.queryData()
  },
  mounted() {},
  methods: {
    //  获取获取员工信息
    async queryInformationData() {
      const res = await pageQuery({ page: 1, pageSize: 100, roleId: 1 })
      // console.log(res.data.list);
      this.tableData = res.data.list
      // this.roles = res.data.list.roles
    },
    // 分页模糊获取员工信息
    async queryData() {
      this.loading = true
      // 处理数据
      const temp = {
        ...this.params,
        ...this.searchForm
      }
      // 遍历对象
      for (const key in temp) {
        // key是一个变量，代表的属性名
        // console.log(key,temp[key]);
        if (!temp[key]) {
          delete temp[key]
        }
      }
      const res = await pageQuery(temp)
      // 加载完毕
      this.loading = false
      this.tableData = res.data.list
      this.total = res.data.total
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      // this.params.pageSize = val;
      this.params = {
        page: 1,
        pageSize: val
      }
    },
    // 当前页码发生变化时的事件处理程序

    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.params.page = val
    },
    add() {
      this.queryInformationData()
      // 清空表单数据
      this.form = {}
      this.title = '添加顾客信息'
      this.visible = true
      this.form.roleId = '1'
    },
    toSave() {
      this.$refs.ruleForm.validate(async(valid) => {
        if (valid) {
          const res = await addUserWidthRole(this.form)
          // 提示用户保存成功，刷新表格数据，关闭模态框
          this.$notify({
            title: '成功',
            message: res.message
          })
          this.queryData()
          this.visible = false
          // this.toClose();
        } else {
          // 校验不通过
          return false
        }
      })
    },
    toSavetwo() {
      this.$refs.ruleForm.validate(async(valid) => {
        if (valid) {
          const res = await saveOrUpdate(this.form)
          // 提示用户保存成功，刷新表格数据，关闭模态框
          this.$notify({
            title: '成功',
            message: res.message
          })
          this.queryData()
          this.visibletwo = false
        } else {
          // 校验不通过
          return false
        }
      })
    },
    // 关闭模态框
    toClose() {
      // 重置表单验证
      this.$refs.ruleForm.resetFields()
    },
    // 编辑按钮的事件处理程序
    toEdit(row) {
      this.queryInformationData()
      // 设置表单数据
      this.form = { ...row }

      this.title = '编辑用户信息'
      this.visibletwo = true
    },

    toSearch() {
      this.queryData()
    },
    toDelete(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await deleteById({ id })
        this.$notify.success({
          title: '成功',
          message: res.success
        })
        this.queryData()
      }).catch(() => {
        this.$notify.info({
          title: '取消',
          message: '已取消删除'
        })
      })
    },
    // 跳转到详情
    toJump(id) {
      this.$router.push({
        path: '/admin/waiter/Details',
        // })
        // this.$router.push('/admin/custome/Details');
        // //传参
        // this.$router.push({
        // path:'/admin/custome/Details',
        query: {
          id: id }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.search {
  display: flex;
  height: 30px;
  margin-bottom: 20px;
  .btn {
    margin-right: 10px;
  }
}
</style>
